﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：LogHelper.cs
// 功能描述：TongTongMall
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using Castle.Core.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using TongTongMall.Dependecy;
using TongTongMall.Extensions;

namespace TongTongMall.Logging
{
    public static class LogHelper
    {
        public static ILogger Logger { get; private set; }

        static LogHelper()
        {
            Logger = IocManager.Instance.IsRegistered(typeof(ILoggerFactory))
                ? IocManager.Instance.Resolve<ILoggerFactory>().Create(typeof(LogHelper))
                : NullLogger.Instance;
        }

        public static void LogException(Exception ex)
        {
            LogException(Logger, ex);
        }

        public static void LogException(ILogger logger, Exception ex, string attachMessage = "")
        {
            var severity = (ex is IHasLogSeverity)
                    ? (ex as IHasLogSeverity).Severity
                    : LogSeverity.Error;
            var titleMessage = ex.Message;
            if (!attachMessage.IsNullOrEmpty())
            {
                titleMessage += "\r\n附加参数：" + attachMessage;
            }
            logger.Log(severity, titleMessage, ex);
        }
    }
}
